import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

public class Main {
	public static void main(String[] args) throws IOException {
		Runtime r = Runtime.getRuntime();
		ArrayList<String> command = processCommand();
		File file = null;
		if (args.length > 1 || args.length == 1 && args[0].contains("help")) {
			showUsage();
			return;
		} else if (args.length == 1 ) {
			file = new File(args[0]);
		}else {
			file = new File("./");
		}
		if (file.exists()) {
			lanch(file,r,command);
		}else {
			System.out.println("your input contains error");
			showUsage();
			return;
		}
	}

	/**
	 * 命令 sed -i -e s/\r$//
	 * @return
	 */
	private static ArrayList<String> processCommand() {
		ArrayList<String> command = new ArrayList<String>();
		command.add("sed");
		command.add("-i");
		command.add("-e");
		command.add("s/\\r$//");
		return command;
	}

	private static void showUsage() {
		System.out.println("usage: ");
		System.out.println("java -jar toLF.jar [file or path]");
		System.out.println("with a file indicate that to transform a file to LF");
		System.out.println("and with a path indicate that to recursively transform file to LF,");
		System.out.println("Nothing append indicate that to transform current path.");
		System.out.println("thanks for you support!");
	}

	private static void lanch(File file, Runtime r, ArrayList<String> command) throws IOException {
		if (file.isDirectory()) {
			File[] files = file.listFiles();
			for (int i = 0; i < files.length; i++) {
				final File f = files[i];
				if (f.isDirectory()) {
					lanch(f,r,command);
				}else {
					transfer(r, command, f);
				}
			}
		}else{
			transfer(r,command,file);
		}
	}

	private static void transfer(Runtime r, List<String> command, File f) throws IOException {
		String aPath = f.getAbsolutePath();
		String[] strings = new String[command.size()+1];
		command.toArray(strings);
		strings[strings.length-1] = aPath;
		System.out.print("command wile be execute: ");
		for (int j = 0; j < strings.length; j++) {
			System.out.print(strings[j]+" ");
		}
		System.out.println();
		final Process exec = r.exec(strings);
		new Thread(new Runnable() {
			public void run() {
				BufferedReader br = new BufferedReader(new InputStreamReader(exec.getErrorStream()));
				String line;
				while (true) {
					try {
						if ((line = br.readLine()) != null) {
							System.out.println(line);
						}else {
							break;
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			}
		}).start();
		new Thread(new Runnable() {
			public void run() {
				BufferedReader br = new BufferedReader(new InputStreamReader(exec.getInputStream()));
				String line;
				while (true) {
					try {
						if ((line = br.readLine()) != null) {
							System.out.println(line);
						}else {
							break;
						}
					} catch (IOException e) {
						e.printStackTrace();
					}
				}

			}
		}).start();
	}
}
